package util

import (
	"fmt"
	"log"
	"math/rand"
	"mp3/entity"
	"os"
	"os/exec"
	"strings"
	"time"
)

func FfmpegDownload(responseEntity entity.ResponseEntity, configEntity entity.DownloadEntity, title string, url string) (bool, string) {
	if len(strings.TrimSpace(url)) == 0 {
		return false, ""
	}
	title1 := title + configEntity.AudioType
	mkdirPath := CombinationFielPath(configEntity.FileDirPath, title1)

	timOut := fmt.Sprintf("%d", configEntity.MaxNum*10000)

	val := fmt.Sprintf("%d", configEntity.MaxNum*10000*5)
	log.Println(title, "[", url, "] FFmpeg 下载开始:", mkdirPath)

	//cmd := exec.Command(strings.TrimSpace(configEntity.FFmpeg), "-timeout", timOut, "-rw_timeout", val, "-i", "\""+strings.TrimSpace(url)+"\"", "-acodec", "mp3", "-ab", "257k", strings.TrimSpace(mkdirPath))
	cmd := exec.Command(strings.TrimSpace(configEntity.FFmpeg), "-timeout", timOut, "-rw_timeout", val, "-i", strings.TrimSpace(url), "-acodec", "mp3", "-ab", "257k", strings.TrimSpace(mkdirPath))
	//cmd := exec.Command(strings.TrimSpace(configEntity.FFmpeg), "-i", strings.TrimSpace(url), "-acodec", "mp3", "-ab", "257k", strings.TrimSpace(mkdirPath))

	defer func() {
		exitCMD(cmd)
	}()

	err := cmd.Run()
	if nil != err {
		log.Println(title, "[", url, "] FFmpeg 下载错误:", err)
		return false, mkdirPath
	} else {
		SleepTime(Default_Sleep_Value)
		log.Println(mkdirPath, url, "--FFMpeg--Over")
	}

	return true, mkdirPath
}

func exitCMD(cmd *exec.Cmd) {
	defer func() { cmd = nil }()
	exec.Command("exit")
	cmd.Run()

}

func ClearAttr(path string, configEntity entity.DownloadEntity, title string, url string) bool {
	newPath := CombinationFielPath(configEntity.FileDirPath, title+".1."+configEntity.AudioType)
	cmd := exec.Command(strings.TrimSpace(configEntity.FFmpeg), "-i", path, "-codec:a", "libmp3lame", "-b:a", "128k", "-map_metadata", "-1", "-y", newPath)
	defer func() {
		exitCMD(cmd)
	}()

	err := cmd.Run()
	if nil != err {
		os.Remove(newPath)
		os.Remove(path)
		log.Println(title, "[", url, "]清除属性错误:", err)
		return false
	}
	RemoveFile(path)
	return true
}

func Sleep(config entity.DownloadEntity, start time.Time) {
	val := random(config)
	sleepAndPrint(val, config, start)
}

/*产生随机数*/
func random(config entity.DownloadEntity) int32 {
	return RandomNum(config.MaxNum, config.MinNum)
}

func RandomNum(maxNum int32, minNum int32) int32 {
	return rand.Int31n(int32(maxNum-minNum)) + minNum
}

/*打印暂停*/
func sleepAndPrint(value1 int32, configEntity entity.DownloadEntity, start time.Time) {
	var i int32 = 0
	elapsed := time.Since(start)
	value := value1 - int32(elapsed.Seconds())
	log.Println(value1, "-------wait value: ", value, "-------Elapsed time:", int32(elapsed.Seconds()))
	if value <= 0 {
		return
	}
	for i = 0; i < value; i++ {
		SleepTime(1)
		if i%25 == 0 {
			fmt.Print("-")
		}
		if i%100 == 0 {
			fmt.Print(fmt.Sprintf("%d", i))
		}
	}
	log.Println(fmt.Sprintf("-%d", i))
}

func SleepTime(val int) {
	//for i := 0; i < val; i++ {
	//	time.Sleep(time.Duration(1) * time.Second)
	//}

}

//func SleepTime1(val int) {
//	for i := 0; i < val; i++ {
//		time.Sleep(time.Duration(1) * time.Second)
//	}
//
//}

func ValudateFile(config entity.DownloadEntity, fp string) bool {
	cmd := exec.Command(strings.TrimSpace(config.FFprobe), "-show_format", strings.TrimSpace(fp))
	err := cmd.Run()
	exec.Command("exit")
	cmd.Run()

	if nil != err {
		log.Println(fp, err)
		os.Remove(fp)
		return false
	}
	return true
}
